import productList from '../../data/productList.js'
var WxParse = require('../../lib/wxParse/wxParse.js');
const productUtil = require('../../utils/productUtil.js');
// pages/goods/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

    id: 0,
    goods: {},

    attribute: [],

    brand: {},

    productList: [],
    incart:false,


    number: 1,
    checkedSpecText: '规格数量选择',
    tmpSpecText: '单位(斤)',
    checkedSpecPrice: 0,
    openAttr: false,
    openShare: false,

    shareImage: '',


    cartNumber: 0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var productId = options.productId;
    var incart = options.incart == 'true'


    var product = productList.filter(x => x.id == productId)[0]
    product.gallery = [product.img.replace(/\?.*/, '')]
    product.picUrl = product.img

    // let { img, price, originPrice, productDescription, productName, productDetial } = product
    
    this.setData({
      goods: product,
      checkedSpecPrice: product.price,
      incart :incart
    })
    WxParse.wxParse('goodsDetail', 'html', product.nodes, this);
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  onClickCartIcon(){
    wx.navigateTo({
      url: '../cart/cart',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var cartProductList = productUtil.getCacheCartProductIdList()
    this.setData({
      cartNumber: cartProductList.size
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    return {
      title: that.data.goods.productName,
      desc: '唯爱与美食不可辜负',
      path: '/pages/goods/goods?productId=' + this.data.id
    }
  },
  switchAttrPop: function () {
    if (this.data.openAttr == false) {
      this.setData({
        openAttr: !this.data.openAttr
      });
    }
  },
  addNumber: function () {
    this.setData({
      number: this.data.number + 1
    });
  },
  cutNumber: function () {
    this.setData({
      number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
    });
  },
  closeAttr: function () {
    this.setData({
      openAttr: false,
    });
  },



  //添加到购物车
  addToCart: function () {
    var that = this;
    if (this.data.openAttr == false) {
      //打开规格选择窗口
      this.setData({
        openAttr: !this.data.openAttr
      });
    } else {

      // todo 添加到购物车
      var goodId = this.data.goods.id
      var number = this.data.number
      productUtil.setCartProductNum(goodId, number)

      var cartProductList = productUtil.getCacheCartProductIdList()
      if (!cartProductList.has(String(goodId))) {
        cartProductList.add(goodId)
      }
      wx.setStorageSync('userCart', [...cartProductList].join(','))
      this.setData({
        cartNumber: cartProductList.size,
        openAttr: false
      })
      wx.showToast({
        title: '添加成功'
      });
    }

  },
})